import React, { Component } from 'react'

export default class Admin extends Component {
    constructor() {
        super()
        this.state = {
            text: {}
        }
    }
    admin() {
        console.log(this.state.text)
        fetch("https://cnodejs.org/api/v1/accesstoken", {
            method: "POST",
            headers: {
                "content-type": "application/json"
            },
            body: JSON.stringify(this.state.text)
        })
            .then(res => res.json())
            .then(post => {
                console.log(post)
                if (post.success) {
                    alert("登陆成功")
                    this.props.history.replace("/Home/" + this.state.text.accesstoken)
                    localStorage.setItem("token", this.state.text.accesstoken)
                    localStorage.setItem("Login", post.loginname)
                } else {
                    alert("token错误，请重新登陆")
                }
            }
            )
    }
    model(e) {
        let obj = { accesstoken: e.target.value }
        this.setState({
            text: obj
        })
        console.log(this.state.text)
    }
    render() {
        return (
            <div>
                请输入token:<input onChange={this.model.bind(this)} type="text" placeholder="请输入token登陆" />
                <button onClick={this.admin.bind(this)} type="button" className="btn btn-success">登陆</button>
            </div>
        )
    }
}
